const express = require("express");
const path = require("path");
const router = express.Router();
const { screenshot, fetchHtmlInfo, fetchHtmlData } = require("./../../utils/robot");
const { download } = require("./../../utils/tool");

//封装函数
function showMessage(message, res) {
  var result = `<script>alert('${message}');history.back()</script>`;
  res.send(result);
}

router.get("/", (req, res, next) => {
  console.log(__dirname, "__dirname");
  // res.render(path.resolve(__dirname, "/views/login.ejs"));
  // res.redirect("/management/login");
  res.render("index.ejs");
});

router.get("/movie", (req, res, next) => {
  console.log(__dirname, "__dirname");
  // res.render(path.resolve(__dirname, "/views/login.ejs"));
  // res.redirect("/management/login");
  res.render("movie.ejs");
});

router.get("/room", (req, res, next) => {
  console.log(__dirname, "__dirname");
  // res.render(path.resolve(__dirname, "/views/login.ejs"));
  // res.redirect("/management/login");
  res.render("room.ejs");
});

// const upload = require("./upload");
router.get("/user", (req, res, next) => {
  res.render("user.ejs");
});
// router.post("/user", upload);

router.get("/login", (req, res, next) => {
  res.render("login.ejs");
});

router.post("/login", (req, res, next) => {
  const { account, password } = req.body;
  if (account && password) {
    res.end("111");
  } else {
    // res.render("error.ejs", {
    //   redirectLink: "/management/login",
    //   message: "账号或密码不能为空"
    // })
    showMessage("账号或密码不能为空", res);
    // res.redirect("/management/login")
  }
  // console.log(req.body, "req");

  // res.render("login.ejs");
});

// 接受并读取 excel 文件 —— node-xlsx

router.get("/screenshot", async (req, res, next) => {
  // screenshot("https://youdata.163.com")
  const html = await fetchHtmlData("https://music.163.com/#/song?id=1338695683")
  // const html = await fetchHtmlInfo("https://movie.douban.com/subject/3001114/?tag=%E7%83%AD%E9%97%A8&from=gaia");
  // download(html)
  res.json({
    code: 200,
    status: "账号或者密码错误",
    data: { html },
  });
})


module.exports = router;
